/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import dto.Category;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author duonghung1369
 */
public class CategoryDAO extends BaseDAO<Category> {

    public CategoryDAO(Class<Category> clazz) {
        super(clazz);
    }

//    public List getAllCategories()  {
//        session = HibernateUtil.getSession();
//        session.beginTransaction();
//        String sql = "from Category";
//        Query query = session.createQuery(sql);
//        List<Category> results = query.list();
//        session.getTransaction().commit();
//
//        return results;
//    }

    public List lazyLoadCategories(Session ses){
        ses.beginTransaction();
        List categories = ses.createCriteria(type).list();
        ses.flush();
        ses.getTransaction().commit();
        return categories;
    }

    public boolean updateDeletedStatus(int id, boolean isDeleted) {
        session.beginTransaction();

        String sql = "update Category set is_deleted = :isdeleted where id = :id or parent_id = :id";
        Query query = session.createQuery(sql);
        query.setBoolean("isdeleted", isDeleted);
        query.setInteger("id", id);
        query.executeUpdate();
        session.flush();
        session.getTransaction().commit();
        return true;
    }
}
